﻿using HeyPiPi_Desk.DomainViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace HeyPiPi_Desk.Domain
{
    /// <summary>
    /// Interaction logic for AuthorityGroupView.xaml
    /// </summary>
    public partial class AuthorityGroupView : UserControl
    {
        AuthorityGroupViewModel  vm = new AuthorityGroupViewModel();
        public AuthorityGroupView()
        {
            InitializeComponent();
            this.DataContext = vm;
        }

        private void Parent_Check(object sender, RoutedEventArgs e)
        {
            CheckBox citem = sender as CheckBox;
            var item = this.vm.ShopAuthorityList.Where(x => x.Name == citem.Content).FirstOrDefault();
            if (item!=null)
            {
                foreach (var child in item.ChildMenu)
                {
                    child.IsChecked = citem.IsChecked.Value;
                }
            }
        }

        private void Child_Check(object sender, RoutedEventArgs e)
        {
            if (sender is CheckBox item)
            {
                //找到父节点
                if (item.ToolTip!=null)
                {
                    int? id = (int?)item.ToolTip;
                    var parent= this.vm.ShopAuthorityList.Where(x => x.Id == id).FirstOrDefault();
                    // 如果子类全部被勾选了，父类也要被勾选，否则不勾选
                    bool cancheck = true;
                    foreach (var child in parent.ChildMenu)
                    {
                        if (child.IsChecked==false)
                        {
                            cancheck = false;
                            break;
                        }
                    }
                    parent.IsChecked = cancheck;
                }
            }
        }
    }
}
